package com.unicloud.watcher;

import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * todo 解析文件
 */
public class FileNameUtil {
    public static void main(String[] args) throws UnsupportedEncodingException {
        // ?gb18030?B?b3NzzsrM4rX3sum94bn7Lm1k?=
        // base
        String name = "=?gb18030?B?b3NzzsrM4rX3sum94bn7Lm1k?=";
        String s = fixFileName(name);
        System.out.println(s);
         name = "xx.md";
         s = fixFileName(name);
        System.out.println(s);
    }

    public static String fixFileName(String srName)  {
//        if (true) {
////            return srName;
////        }

        if(srName.matches("\\?.*\\?B\\?.*\\?=")){
            // 提取byte
            int i = srName.indexOf("B?");
            String base64 =  srName.substring(i+2,srName.length() - 2);
            String charsetName =  srName.substring(1,i);
            byte[] decode = Base64.getDecoder().decode(base64);
            try {
                return new String(decode,"gb18030");
            } catch (Exception ex){
                ex.printStackTrace();
            }
            return base64;
        }
        return srName;
    }
}
